package com.eastfair.home.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dao.ReportManageAuditHistoryMapper;
import com.eastfair.home.dto.ReportManageAuditHistoryDTO;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.home.entity.ReportManageAuditHistory;
import com.eastfair.home.service.ReportManageAuditHistoryService;
import com.eastfair.home.vo.ReportManageAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆管理费审核记录历史
 * </p>
 *
 * @author linan
 * @date 2023-04-18
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportManageAuditHistoryServiceImpl extends SuperServiceImpl<ReportManageAuditHistoryMapper, ReportManageAuditHistory> implements ReportManageAuditHistoryService {

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportManageAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportManageAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportManageAuditHistoryVO> queryReportManageAuditHistory(ReportManageAuditHistoryDTO reportManageAuditHistoryDTO) {
        LambdaQueryWrapper<ReportManageAuditHistory> wrapper = new QueryWrapper<ReportManageAuditHistory>().lambda()
                .eq(ReportManageAuditHistory::getIsDeleted, BusinessConstant.NO)
                .eq(ReportManageAuditHistory::getReportSpaceId, reportManageAuditHistoryDTO.getReportSpaceId())
                .orderByDesc(ReportManageAuditHistory::getAuditTime);
        List<ReportManageAuditHistory> list = list(wrapper);
        return ConvertUtil.convertList(list, ReportManageAuditHistoryVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHistoryByReportSpaceAudit(ReportSpaceAuditDTO reportSpaceAuditDTO, LocalDateTime submitTime) {
        // 插入审核记录
        ReportManageAuditHistory reportManageAuditHistory = new ReportManageAuditHistory();
        reportManageAuditHistory.setAuditState(reportSpaceAuditDTO.getManageAuditStatus().getCode());
        reportManageAuditHistory.setAuditOpinion(reportSpaceAuditDTO.getManageAuditOpinion());
        reportManageAuditHistory.setAuditTime(LocalDateTime.now());
        reportManageAuditHistory.setAuditUserId(ContextUtil.getUserId());
        reportManageAuditHistory.setAuditUserName(ContextUtil.getRealName());
        reportManageAuditHistory.setReportSpaceAuditId(reportSpaceAuditDTO.getId());
        reportManageAuditHistory.setReportSpaceId(reportSpaceAuditDTO.getReportSpaceId());
        reportManageAuditHistory.setSubmitTime(submitTime);
        save(reportManageAuditHistory);
        return true;
    }

    @Override
    public void asyncSaveHistoryByReportSpaceAudit(ReportSpaceAuditDTO reportSpaceAuditDTO, LocalDateTime submitTime) {
        threadPoolTaskExecutor.execute(() -> saveHistoryByReportSpaceAudit(reportSpaceAuditDTO, submitTime));
    }
}
